import matplotlib.pyplot as plt
from matplotlib.ticker import NullLocator
from typing import TYPE_CHECKING
import math

import visualizer.common as v
from visualizer.board_visualizer import visualize_board
from container import v_get_face_color_by_td, OPTIONS

from solver.SBoard import SBoard
from solver.SComponent import SComponent

if TYPE_CHECKING:
    from puzzle.board import Board
    from matplotlib.axes import Axes
    from puzzle.component import Transform


def visualize_comp(comp: "Transform", opath: str) -> "Axes":
    ax: "Axes"
    
    fig, ax = plt.subplots()
    # fig.canvas.manager.set_window_title(board.name) # type: ignore
    
    v.prepare_ax(ax)
    # v.plot_board(ax, board)
    
    ax.xaxis.set_major_locator(NullLocator())
    ax.yaxis.set_major_locator(NullLocator())
    plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) # https://blog.csdn.net/jifaley/article/details/79687000
    plt.margins(0, 0)

    for i in range(0, len(comp.td)):
        v.draw_polygon(
            ax,
            comp.td[i].vertices,
            facecolor = (0.97, 0.51, 1),
            edgecolor = (0, 0, 0),
            linewidth = 1.5
        )
    
    fig.savefig(opath, dpi = 300, transparent = True, pad_inches = 0)
        
    return ax


def visualize_transforms(transforms: list["Transform"]) :
    n = len(transforms)
    
    cols = 3
    rows = math.ceil(n / cols)
    
    fig, axs = plt.subplots(rows, cols)
    axs = axs.flatten()
    fig.canvas.manager.set_window_title(f"{n} Transforms") # type: ignore

    for i in range(n):
        ax: "Axes" = axs[i]
        comp = transforms[i]
        
        v.prepare_ax(ax)
    
        origin = comp.td[0]
        v.draw_polygon(
            ax,
            origin.vertices,
            OPTIONS.V_UNS_COLOR,
        )
        
        for i in range(1, len(comp.td)):
            v.draw_polygon(
                ax,
                comp.td[i].vertices,
                (0, 0, 0)
            )
    
    for i in range(n, len(axs)):
        fig.delaxes(axs[i])
    
    plt.tight_layout()

def visualize_components(comps: list[list["Transform"]]):
    n = len(comps)
    
    cols = 3
    rows = math.ceil(n / cols)
    
    fig, axs = plt.subplots(rows, cols)
    axs = axs.flatten()
    fig.canvas.manager.set_window_title(f"{n} Components") # type: ignore

    for i in range(n):
        ax: "Axes" = axs[i]
        comp = comps[i][0]
        
        v.prepare_ax(ax)
    
        for i in range(1, len(comp.td)):
            v.draw_polygon(
                ax,
                comp.td[i].vertices,
                facecolor = (0.97, 0.51, 1),
                edgecolor = (0, 0, 0),
                linewidth = 1.5
            )
        
        origin = comp.td[0]
        v.draw_polygon(
            ax,
            origin.vertices,
            facecolor = (0.97, 0.51, 1),
            edgecolor = (0, 0, 0),
            linewidth = 1.5
        )
    
    for i in range(n, len(axs)):
        fig.delaxes(axs[i])
    
    plt.tight_layout()